Skip to content
View Article Network

Reasons why ASPNETCORE_ENVIRONMENT fails to take effect

I've been a bit lazy with writing notes lately, so I'll just record this briefly.

I previously wrote an article titled "A Brief Discussion on Environment Name Configuration and Application in ASP.NET Core", but I recently encountered a project that required setting the environment via Windows environment variables, so I referred to the Microsoft article "Use multiple environments in ASP.NET Core - Set the environment variable globally".

After configuring it, I found that the project was still only reading appsettings.json and not appsettings.{Environment}.json. Neither restarting the website nor restarting the application pool had any effect. Since this was a client's environment and I wasn't familiar with it, I didn't resort to the "restart everything to fix everything" approach immediately.

Later, I found this article: "How to set environment variables for .NET Core deployment on IIS", and realized that IIS needs to be restarted for the environment variable settings to take effect. The problem was solved after executing the following command:

bash
iisreset /restart

TIP

You may need administrator privileges to execute the command above.

Change Log

  • 2024-09-19 Initial version created.